package pl.register.android.activities.program.builder;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import pl.register.android.R;
import pl.register.android.activities.program.logic.ProgramPresenter;
import pl.register.android.chapter.ChapterTitle;
import pl.register.android.classes.ClassesTopic;
import pl.register.android.lesson.LessonProgram;


/**
 * @author Mateusz Strzelecki
 * @since 2013-04-12
 */
public class HolidayProgramEditorDialogBuilder extends AlertDialog.Builder {

    private EditText etTopic;

    private EditText etChapter;


    private ProgramPresenter presenter;

    public HolidayProgramEditorDialogBuilder(Context context, LessonProgram lessonProgram, ProgramPresenter presenter) {
        super(context);
        this.presenter = presenter;

        onCreateContent(context, lessonProgram);
        setupDialog(lessonProgram);
        populateFields(lessonProgram);
    }

    private void onCreateContent(Context ctx, LessonProgram lessonProgram) {
        LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(ctx.LAYOUT_INFLATER_SERVICE);
        View dialogLayout = inflater.inflate(R.layout.dialog_program_editor_layout, null);
        etTopic = (EditText) dialogLayout.findViewById(R.id.topic_1_edit_text);
        etChapter = (EditText) dialogLayout.findViewById(R.id.chapter_edit_text);

        View secondTopicRow = dialogLayout.findViewById(R.id.second_topic_row);
        secondTopicRow.setVisibility(View.GONE);
        View homeworkRow = dialogLayout.findViewById(R.id.homework_row);
        homeworkRow.setVisibility(View.GONE);

        setView(dialogLayout);
    }

    private void setupDialog(final LessonProgram lessonProgram) {
        setTitle(R.string.dialog_title_edit_program);
        setNegativeButton(R.string.btn_name_cancel, null);
        setPositiveButton(R.string.btn_name_save, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                presenter.saveHolidayLessonProgram(getValuesFromFields(lessonProgram));
            }
        });
    }

    private void populateFields(LessonProgram lessonProgram) {
        if (lessonProgram.getClassesTopics().size() == 1) {
            ClassesTopic classesTopic = lessonProgram.getClassesTopics().get(0);
            etTopic.setText(classesTopic.getTopic());
        }
        ChapterTitle chapterTitle = lessonProgram.getChapterTitle();
        etChapter.setText(chapterTitle.getTitle());
    }

    private LessonProgram getValuesFromFields(LessonProgram lessonProgram) {
        if (lessonProgram.getClassesTopics().size() == 1) {
            ClassesTopic classesTopic = lessonProgram.getClassesTopics().get(0);
            classesTopic.setTopic(etTopic.getText().toString());
        }
        ChapterTitle chapterTitle = lessonProgram.getChapterTitle();
        chapterTitle.setTitle(etChapter.getText().toString());
        return lessonProgram;
    }

}